/**
 * AliperLine是一条具有最大size的点组成的连线
 * 当addPoint(x,y)超过最大尺寸后，会自动丢弃最前面的点
 * 使得AliperLine的长度始终不超过最大尺寸
 *
 */
function AliperLine(lineId,color){
	this.needPointId = 0;
	this.points=[];
	this.lineId = lineId;
	this.color = color;
	this.addPoint = function(x,y){
		this.points[this.points.length] = [x,y];
		this.needPointId++;
	}
	this.shiftTo = function(x){
		while(this.points[0][0] < x){
			delete this.points[0];
			this.points.shift();
		}
	}
	this.getSize = function(){
		return this.points.length;
	}
	this.getEndPoint = function(){
		return this.points[this.points.length - 1];
	}
	this.getPoint = function(index){
		return this.points[index];
	}
}
function AliperChart(div,agentId,chartId){
	this.div = div;
	this.agentId = agentId;
	this.chartId = chartId;
	
	this.startx = new Date().getTime();
        //重要:决定x族的时间长度
	this.lengthx = 60000;
	this.starty = 0;
	this.lengthy = 100;

	this.myChart = new JSChart(div, 'line');
	this.labelPoints = [[0,0]];
	this.initMyChart = function(lastPoint){
		this.myChart.setAxisPaddingBottom(40);
		this.myChart.setTextPaddingBottom(10);
		this.myChart.setAxisValuesNumberY(5);
		if(this.starty != null){
			this.myChart.setIntervalStartY(this.starty);
			if(this.lengthy != null)
				this.myChart.setIntervalEndY(this.starty + this.lengthy);
		}
		if(this.startx != null){
			this.myChart.setIntervalStartX(this.startx);
			if(this.lengthx != null)
				this.myChart.setIntervalEndX(this.startx + this.lengthx);
		}
		this.myChart.setAxisValuesNumberX(7);
		if(lastPoint != null){
			if(lastPoint[0] - this.labelPoints[this.labelPoints.length - 1][0] > this.lengthx / 6){
				var date = new Date();
				date.setTime(lastPoint[0]);
				this.labelPoints[this.labelPoints.length] = [lastPoint[0], date.getHours() + ":"+date.getMinutes() + ":" + date.getSeconds()];
				delete date;
			}
			while(this.labelPoints.length > 0 && this.labelPoints[0][0] < this.startx){
				this.labelPoints.shift();
			}
		}
		for(index in this.labelPoints){
			this.myChart.setLabelX([this.labelPoints[index][0], this.labelPoints[index][1]]);
		}
		
		this.myChart.setShowXValues(false);
		this.myChart.setTitleColor('#454545');
		this.myChart.setAxisValuesColor('#454545');
		this.myChart.setFlagColor('#9D16FC');
		this.myChart.setFlagRadius(3);
		this.myChart.setBackgroundImage('images/chart_bg.jpg');
		this.myChart.setTitle(this.chartId);
		this.myChart.setSize(616, 321);
	}
	this.initMyChart(null);
	
	this.interval = 1000;
	this.lines = [];
	this.show = function(){
		var obj = this;
		window.setInterval(function(){
				obj.run();
		},0);
	}
	this.run = function(){
			var obj = this;
			for(index in this.lines){
				ajax(function(message){
					obj.callback(obj.lines[index],message);
				},"/aliper-server/AliperPointer?agent="+this.agentId+"&chart="+this.chartId+"&line="+this.lines[index].lineId+"&point="+this.lines[index].needPointId,true);
			}
	}
	this.processPoint = function(line,mess){
		if(mess.indexOf("@") > 0){
			var arr = mess.split("@");
			var x = parseInt(arr[0]);
			var y = parseFloat(arr[1]);
			line.addPoint(x,y);
		// 舍弃最前面超出图片的点
		//	this.startx = x - this.lengthx;
			this.startx = this.startx > x ? x : this.startx;
			this.lengthx = this.lengthx > x - this.startx ? this.lengthx : x - this.startx;
			
		//	line.shiftTo(this.startx);
		} 
	}
	this.callback = function(line,mess){
	    if(mess.indexOf("null") == 0){//暂时没有最新数据
	    	return;
	    }
		else if( mess.indexOf("message:") == 0){
			alert("Server Message:" + mess.substring(8));
		} else if(mess.indexOf("#") > 0){
			var arr = mess.split("#");
			for(index in arr){
				this.processPoint(line,arr[index]);
			}
		} else {
			this.processPoint(line,mess);
		}
		if(line.getSize() > 1){
			//jschart框架不释放内存，所以反复调用draw会有性能问题，索性重新申请了
			delete this.myChart;
			this.myChart = new JSChart(div, 'line');
			this.initMyChart(line.points[line.points.length - 1]);
			for(index in this.lines){
				this.myChart.setDataArray(this.lines[index].points, this.lines[index].lineId);
			   this.myChart.setLineColor(this.lines[index].color, this.lines[index].lineId);
				this.myChart.setLineWidth(1, this.lines[index].lineId);
			}
			this.myChart.setIntervalStartX(this.startx);
			this.myChart.setIntervalEndX(this.startx + this.lengthx);
			
			this.myChart.draw();
		}
	}
	
	this.addLine = function(title){
		var color;
		if(this.lines.length == 0){
			color = '#5555FF';
		} else if(this.lines.length == 1){
			color = '#A4D314';
		} else {
			color = '#E38EFF';
		}
		this.lines[this.lines.length] = new AliperLine(title,color);	
	}
}

